/*
 * Copyright (c) 田梓萱 2021-2022.
 * All Rights Reserved.
 * All codes are protected by China's regulations on the protection of computer software, and infringement must be investigated.
 * 版权所有 (c) 田梓萱 2021-2022.
 * 所有代码均受中国《计算机软件保护条例》保护，本项目已获得软著.
 */

package com.xcl.calculator.Net;

import com.xcl.calculator.XLog;
import ohos.hiviewdfx.HiLog;
import ohos.hiviewdfx.HiLogLabel;
import ohos.net.ConnectionProperties;
import ohos.net.NetCapabilities;
import ohos.net.NetHandle;

import static com.xcl.calculator.Preference_RW.NetStatus;

/**
 * The type Net status callback.
 *
 * @author Xcl
 * @date 2022 /2/18
 * @package com.xcl.calculator.Net
 */
public class NetStatusCallback extends ohos.net.NetStatusCallback {
    /**
     * The constant label.
     */
    private static final HiLogLabel label = new HiLogLabel(HiLog.LOG_APP, 0x00234, "NetStatusCallback");

    /**
     * TODO:自定义网络状态一览表
     * 0.初始化值=0
     * 1.网络连接成功=1
     * 2.网络可用[使用WiFi]=2
     * 3.网络可用[使用数据网络]=3
     * 4.网络可用[其它网络]=4
     * 5.网络状态变化中[网络连接的属性被修改]=5
     * 6.网络不可用[网络正在丢失连接]=6
     * 7.网络不可用[网络已断开连接]=7
     * 8.网络不可用[网络连接超时或网络不可达]=8
     * 9.网络未知[访问的网络阻塞状态发生变化]=9
     * 10.网络未知>=10
     * TODO:综上0为初始，1为连接成功，[2,4]为真实可用，[6,8]为绝对不可用
     *
     * @param handle the handle
     */
    @Override
    public void onAvailable(NetHandle handle) {
        super.onAvailable(handle);
        NetStatus = 1;
        XLog.info(label, "网络连接成功");
    }

    /**
     * On blocked status changed.
     *
     * @param handle  the handle
     * @param blocked the blocked
     */
    @Override
    public void onBlockedStatusChanged(NetHandle handle, boolean blocked) {
        super.onBlockedStatusChanged(handle, blocked);
        XLog.info(label, "访问的网络阻塞状态发生变化");
        NetStatus = 9;
    }

    /**
     * On losing.
     *
     * @param handle      the handle
     * @param maxMsToLive the max ms to live
     */
    @Override
    public void onLosing(NetHandle handle, long maxMsToLive) {
        super.onLosing(handle, maxMsToLive);
        XLog.info(label, "网络正在丢失连接");
        NetStatus = 6;
    }

    /**
     * On lost.
     *
     * @param handle the handle
     */
    @Override
    public void onLost(NetHandle handle) {
        super.onLost(handle);
        XLog.info(label, "网络已断开连接");
        NetStatus = 7;
    }

    /**
     * On unavailable.
     */
    @Override
    public void onUnavailable() {
        super.onUnavailable();
        XLog.info(label, "网络连接超时或网络不可达");
        NetStatus = 8;
    }

    /**
     * On connection properties changed.
     *
     * @param handle               the handle
     * @param connectionProperties the connection properties
     */
    @Override
    public void onConnectionPropertiesChanged(NetHandle handle, ConnectionProperties connectionProperties) {
        super.onConnectionPropertiesChanged(handle, connectionProperties);
        XLog.info(label, "网络连接的属性被修改");
        NetStatus = 5;
    }

    /**
     * On capabilities changed.
     *
     * @param handle              the handle
     * @param networkCapabilities the network capabilities
     */
    @Override
    public void onCapabilitiesChanged(NetHandle handle, NetCapabilities networkCapabilities) {
        super.onCapabilitiesChanged(handle, networkCapabilities);
        XLog.info(label, "网络状态变化但仍旧是可用状态");
        // 网络连接成功，并且可以上网
        if (networkCapabilities.hasCap(NetCapabilities.NET_CAPABILITY_VALIDATED))
            if (networkCapabilities.hasBearer(NetCapabilities.BEARER_WIFI) ||
                    networkCapabilities.hasBearer(NetCapabilities.BEARER_WIFI_AWARE)) {
                XLog.info(label, "使用WiFi");
                NetStatus = 2;
            } else if (networkCapabilities.hasBearer(NetCapabilities.BEARER_CELLULAR) ||
                    networkCapabilities.hasBearer(NetCapabilities.BEARER_ETHERNET)) {
                XLog.info(label, "使用数据网络");
                NetStatus = 3;
            } else {
                // 其它网络，包括蓝牙、VPN、LoWPAN等
                XLog.info(label, "其它网络");
                NetStatus = 4;
            }
    }
}

